package com.library.books.service.impl;

import com.library.books.domain.BooksCopy;
import com.library.books.domain.BorrowRecord;
import com.library.books.mapper.GDXXPushServiceMapper;
import com.library.books.service.IGDXXPushService;
import com.library.common.gdxx.domain.req.BookBorrowPushReq;
import com.library.common.gdxx.domain.req.BookInfoPushReq;
import com.library.common.gdxx.domain.resp.GDXXResponse;
import com.library.common.gdxx.push.InternalPush;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Package: com.library.books.service.impl
 * @ClassName: GDXXPushServiceImpl
 * @Author: Yin
 * @Date: 17:07
 * @Description: 此文件描述信息
 */
@Slf4j
@Service
public class GDXXPushServiceImpl implements IGDXXPushService {

    @Resource
    private GDXXPushServiceMapper gdxxPushServiceMapper;

    @Resource
    private InternalPush internalPush;

    /**
     * 根据副本id-查询数据库将推送的参数查询出来
     * @param copyIds 查询的副本id
     * @return 查询结果
     */
    @Override
    @Transactional(readOnly = true , isolation = Isolation.READ_COMMITTED)
    public List<BookInfoPushReq> selectPushBookInfoParams(List<Long> copyIds) {
        return gdxxPushServiceMapper.selectPushBookInfoParams(copyIds);
    }


    /**
     * 根据借阅id-查询数据库将推送的参数查询出来
     *  - 借阅时查询
     *  - 续借时查询
     *  - 归还时查询
     *  不区分状态
     * @param borrowIds 借阅id
     * @return 返回结果
     */
    @Override
    @Transactional(readOnly = true , isolation = Isolation.READ_COMMITTED)
    public List<BookBorrowPushReq> selectPushBookBorrowInfoParams(List<Long> borrowIds) {
        return gdxxPushServiceMapper.selectPushBookBorrowInfoParams(borrowIds);
    }


    @Override
    public boolean pushBookInfoParams(List<Long> copyIds) {
        try{
            List<BookInfoPushReq> bookInfoPushReqs = this.selectPushBookInfoParams(copyIds);
            if(bookInfoPushReqs.isEmpty()) {
                log.error("广东学心推送新副本信息异常:{}，数据库未查询出数据",bookInfoPushReqs);
                return false;
            }
            log.info("广东学心推送新副本信息列表:{}",bookInfoPushReqs);
            // 批量推送
            GDXXResponse gdxxReesponse = internalPush.bookInfo(bookInfoPushReqs);
            if(gdxxReesponse.getCode() == 200) {
                log.info("广东学心推送新副本信息成功:{}，数据条目{}条",gdxxReesponse,bookInfoPushReqs.size());
                return true;
            }else{
                log.error("广东学心推送新副本信息异常:{}，数据条目{}条",gdxxReesponse,bookInfoPushReqs.size());
            }
        }catch (Exception e){
            log.error("广东学心推送新副本信息失败:{}",e.getMessage());
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据借阅id-查询数据库将推送的参数查询出来
     *  - 借阅时查询
     *  - 续借时查询
     *  - 归还时查询
     *  不区分状态
     * @param borrowIds 借阅id
     * @return 返回结果
     */
    @Override
    public boolean pushBookBorrowInfoParams(List<Long> borrowIds) {
        try {
            List<BookBorrowPushReq> bookBorrowPushReqs = this.selectPushBookBorrowInfoParams(borrowIds);
            if(bookBorrowPushReqs.isEmpty()) {
                log.error("广东学心推送借阅信息异常:{}，数据库未查询出数据",bookBorrowPushReqs);
                return false;
            }
            log.info("广东学心推送借阅信息列表:{}",bookBorrowPushReqs);
            // 推送消息给校方平台接口
            GDXXResponse gdxxReesponse = internalPush.bookBorrowInfo(bookBorrowPushReqs);
            if(gdxxReesponse.getCode() == 200) {
                log.info("广东学心推送借阅消息成功:{}，数据条目{}条",gdxxReesponse,bookBorrowPushReqs.size());
                return true;
            }else{
                log.info("广东学心推送借阅消息异常:{}，数据条目{}条",gdxxReesponse,bookBorrowPushReqs.size());
            }
        }catch (Exception e) {
            log.error("广东学心推送借阅信息失败:{}",e.getMessage());
            e.printStackTrace();
        }
        return false;
    }
    
}
